package com.insta.formation.calendar.service.ws;

import java.util.Date;

import javax.jws.WebService;

import com.insta.formation.calendar.manager.ICalendarManager;
import com.insta.formation.calendar.manager.IUserManager;
import com.insta.formation.calendar.model.Event;
import com.insta.formation.calendar.model.Person;
import com.insta.formation.calendar.service.IUserService;


/**
 *  @See {@link IUserService}
 */
@WebService(endpointInterface = "com.insta.formation.calendar.service.IUserService")
public class UserServiceWS implements IUserService {
	
	private IUserManager userManager;
	private ICalendarManager calendarManager;
	
	public UserServiceWS(IUserManager userManager, ICalendarManager calendarManager) {
		this.userManager = userManager;
		this.calendarManager = calendarManager;
	}

	/**
	 *  @See {@link IUserService}
	 */
	public boolean createUser(String email, String firstName, String lastName) {
		return userManager.createUser(email, firstName, lastName);
	}

	/**
	 *  @See {@link IUserService}
	 */
	public boolean updateUser(String email, String firstName, String lastName) {
		Person person = userManager.retreiveUser(email);
		if (person != null) {
			person.setFirstName(firstName);
			person.setLastName(lastName);
			return userManager.updateUser(person);
		}
		return false;
	}

	/**
	 *  @See {@link IUserService}
	 */
	public boolean deleteUser(String email) {
		return userManager.deleteUser(email);
	}

	/**
	 *  @See {@link IUserService}
	 */
	public boolean createCalendar(String emailOwner, String calendarName) {
		Person owner = userManager.retreiveUser(emailOwner);
		if (owner != null) {
			return calendarManager.createCalendar(calendarName, owner);
		}
		
		return false;
	}

	/**
	 *  @See {@link IUserService}
	 */
	public boolean createEvent(String emailOwner, String calendarName,
			String nameEvent, Date startDate, Date endDate) {
		
		Person owner = userManager.retreiveUser(emailOwner);
		if (owner != null) {		
			Event event = new Event();
			event.setName(nameEvent);
			event.setStartDate(startDate);
			event.setEndDate(endDate);
			return calendarManager.createEvent(calendarName, owner, event);
		}
		return false;
	}
	
	/**
	 *  @See {@link IUserService}
	 */
	public boolean updateEvent(String emailOwner, String calendarName,
			String eventName, String newEventName, Date startDate, Date endDate) {

		Person owner = userManager.retreiveUser(emailOwner);
		if (owner != null) {		
			return calendarManager.updateEvent(calendarName, owner, eventName, newEventName, startDate, endDate);
					
		}
		return false;
	}
	
	
	/**
	 *  @See {@link IUserService}
	 */
	public boolean deleteEvent(String emailOwner, String calendarName,
			String nameEvent)
	{
		Person owner = userManager.retreiveUser(emailOwner);
		if (owner != null) {		
			return calendarManager.deleteEvent(calendarName, owner, nameEvent);
		}
		return false;
	}

	/**
	 *  @See {@link IUserService}
	 */
	public boolean addEventGuest(String emailOwner, String calendarName,
			String nameEvent, String emailGuest) {
		Person owner = userManager.retreiveUser(emailOwner);
		Person guest = userManager.retreiveUser(emailGuest);
		if ((owner != null) && (guest != null)) {
			calendarManager.addEventGuest(owner, calendarName, nameEvent, guest);
		}
		return false;
	}


	/**
	 *  @See {@link IUserService}
	 */
	public boolean deleteEventGuest(String emailOwner, String calendarName,
			String nameEvent, String emailGuest) {
		
		Person owner = userManager.retreiveUser(emailOwner);
		Person guest = userManager.retreiveUser(emailGuest);
		if ((owner != null) && (guest != null)) {
			calendarManager.deleteEventGuest(owner, calendarName, nameEvent, guest);
		}
		return false;
	}	

}
